class HTML2Markup(object):
	html_bool = {
		'true': True,
		't': True,
		'1': True,
		'yes': True,
		'y': True,
		'false': False,
		'f': False,
		'0': False,
		'no': False,
		'n': False
	}
	
	_join = ''.join

	def parse_bool(self, value):
		'''Parse a string or integer to determine truthiness.
		Defaults to True (for the case of valueless attributes, i.e. <span narrow></span>'''
		value = unicode(value).lower()
		return self.html_bool.get(value, True)
	
	def handle_tag(self, data, tag, attrs):
		if tag == 'hr':
			data += '-' * 40 + '\r\n'
		return data, attrs
	
	def handle_tag_close(self, data, tag, attrs):
		return data, attrs
	
	def handle_attrs(self, attrs, current_attrs):
		data = []
		for name, value in attrs.iteritems():
			if name == 'align':
				pass
			elif name == 'narrow':
				if value:
					data.append('[size=12]')
				else:
					data.append('[/size]')
			elif name == 'bold':
				if value:
					data.append('[b]')
				else:
					data.append('[/b]')
			elif name == 'double-height':
				if value:
					data.append('[size=20]')
				else:
					data.append('[/size]')
			elif name == 'double-width':
				if value:
					data.append('[size=20][b]')
				else:
					data.append('[/b][/size]')
			elif name == 'underline':
				pass
			elif name == 'rotate':
				pass
			elif name == 'flip':
				pass
			elif name == 'invert':
				pass
			elif name == 'smooth':
				pass
		
		if data:
			return self._join(data)
		return ''
	
	def htmlout(self, text):
		return text.replace('\r', '')
	
	@staticmethod
	def finish(text):
		while text[:2] == '[/':
			_, _, text = text.partition(']')
		return text

	def convert_to_bytes(self, unihtml):
		return unihtml.encode('utf-8')